package Dao.impl;

import Dao.BaseDao;
import entity.Seat;
import entity.Ticket;
import entity.Train;
import entity.User;
import untils.Msg;
import untils.SqlCollection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

public class BaseDaoImpl implements BaseDao {
    private SqlCollection sqlCollection = new SqlCollection();
    private Msg<User> userMsg = new Msg<>();
    @Override
    public Msg<User> login(String phone) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        User user = null;
        Connection conn = null;

        try {
            //1.连接数据库
            conn = sqlCollection.openConnection();
            //sql查询语句
            String sql = "select *from user where phone = ?";
            //创建预编译对象
            pst = conn.prepareStatement(sql);
            //给占位符赋值
            pst.setString(1,phone);
            //执行sql
            rs = pst.executeQuery();
            while (rs.next()){
                user  = new User();
                user.setUser_id(rs.getInt("user_id"));
                user.setUserName(rs.getString("username"));
                user.setPhone(rs.getString("phone"));
                user.setPassword(rs.getString("password"));
                user.setReal_name(rs.getString("real_name"));
                user.setEmail(rs.getString("email"));
                user.setRole(rs.getString("role"));
                user.setIdCard(rs.getString("idcard"));
            }
            userMsg.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }

        return userMsg;
    }

    @Override
    public Msg<User> register(User user) {
        PreparedStatement pst = null;
        Connection conn = null;

        try{
            //1.连接数据库
            conn = sqlCollection.openConnection();
            String sql = "insert into user(user_id,username, password, real_name, phone, email,idcard) values(?,?,?,?,?,?,?)";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,user.getUser_id());
            pst.setString(2,user.getUserName());
            pst.setString(3,user.getPassword());
            pst.setString(4,user.getReal_name());
            pst.setString(5,user.getPhone());
            pst.setString(6,user.getEmail());
            pst.setString(7,user.getIdCard());
            int res =  pst.executeUpdate();
            if (res > 0) {
                userMsg.setMsg("SUCCEED");
            } else {
                userMsg.setMsg("ERROR");
            }

        }catch (Exception e ){
            e.printStackTrace();
        }finally {
           sqlCollection.closeConnection(conn,pst);
        }
        return userMsg;
    }

    /**
     * 更新用户个人信息
     *
     *
     * @return Msg<User> 更新结果的封装对象，包含更新是否成功以及更新后的用户信息
     * @throws Exception 更新过程中可能发生的异常
     */
    @Override
    public Msg<User> updatePersonalData(Integer  id, String username,String password,String real_name,String phone,String email) {
        PreparedStatement pst = null;
        Connection conn = null;
        ResultSet rs =null;

        try{
            conn = sqlCollection.openConnection();
            String sql = "UPDATE user SET username = ?, password = ?, real_name = ?, phone = ?, email = ? WHERE user_id = ?";
            pst = conn.prepareStatement(sql);

            pst.setString(1,username);
            pst.setString(2,password);
            pst.setString(3,real_name);
            pst.setString(4,phone);
            pst.setString(5,email);
            pst.setInt(6,id);
            userMsg.setData(new User(id,username,password,real_name,phone,email,userMsg.getData().getRole()));
            int res = pst.executeUpdate();
            if(res>0){
                userMsg.setMsg("SUCCEED");
            }else {
                userMsg.setMsg("ERROR");
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return userMsg;
    }

    @Override
    public int findUserByPhone(String phone) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;

        try{
            conn = sqlCollection.openConnection();
            String sql = "select * from user where phone = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,phone);
            rs = pst.executeQuery();
            int remainNum = 0 ;
            if (rs.next()){
                remainNum =  rs.getInt(1);
            }
            return remainNum;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }
        return 0;
    }

    @Override
    public Msg<ArrayList<Train>> findAllTrainData() {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        Msg<ArrayList<Train>> msg= new Msg<>();
        ArrayList<Train> trains = new ArrayList<>();
        try {
            conn = sqlCollection.openConnection();
            String sql = "select * from trains inner join seat on trains.train_id = seat.train_id";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            Train train = null;
            Seat seat = null;
            while (rs.next()){
                train = new Train();
                seat = new Seat();
                train.setTrainId(rs.getInt(1));
                train.setTrainName(rs.getString(2));
                train.setDepartureTime(rs.getDate(3));
                train.setArrivalTime(rs.getDate(4));
                train.setStartStation(rs.getString(5));
                train.setEndStation(rs.getString(6));
                train.setCarStatus(rs.getString(7));
                train.setTicket_status(rs.getInt(8));
                seat.setSeatId(rs.getInt(9));
                seat.setTrainId(rs.getInt(10));
                seat.setType(rs.getString(11));
                seat.setPrice(rs.getString(12));
                seat.setNumber(rs.getInt(13));
                train.setSeat(seat);
                trains.add(train);
            }
            msg.setData(trains);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }
        return msg;
    }
}
